package com.stylefeng.guns.modular.biz.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.common.constant.factory.PageFactory;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.exception.BussinessException;
import com.stylefeng.guns.common.persistence.dao.ProductFlowMapper;
import com.stylefeng.guns.common.persistence.dao.ProductInfoMapper;
import com.stylefeng.guns.common.persistence.dao.RoleMapper;
import com.stylefeng.guns.common.persistence.model.OperationLog;
import com.stylefeng.guns.common.persistence.model.ProductFlow;
import com.stylefeng.guns.common.persistence.model.ProductInfo;
import com.stylefeng.guns.common.persistence.model.Role;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.biz.dao.ProductDao;
import com.stylefeng.guns.modular.biz.warpper.ProductWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 产品信息控制器
 *
 * @author fengshuonan
 * @Date 2017-10-15 14:16:34
 */
@Controller
@RequestMapping("/product")
public class ProductController extends BaseController {

    private String PREFIX = "/biz/product/";

    @Resource
    private ProductDao productDao;

    @Resource
    ProductInfoMapper productInfoMapper;

    @Resource
    ProductFlowMapper productFlowMapper;

    /**
     * 跳转到产品信息首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "product.html";
    }

    /**
     * 跳转到添加产品信息
     */
    @RequestMapping("/product_add")
    public String productAdd() {
        return PREFIX + "product_add.html";
    }

    /**
     * 跳转到添加产品信息
     */
    @RequestMapping("/product_flow_add/{productId}")
    public String productFlowAdd(@PathVariable Integer productId, Model model ) {
        if (ToolUtil.isEmpty(productId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        ProductInfo  p=this.productInfoMapper.selectById(productId);
        model.addAttribute("product",p);
        LogObjectHolder.me().set(p);
        return PREFIX + "product_flow_add.html";
    }

    /**
     * 跳转到修改产品信息
     */
    @RequestMapping("/product_update/{productId}")
    public String productUpdate(@PathVariable Integer productId, Model model) {
        if (ToolUtil.isEmpty(productId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        ProductInfo  p=this.productInfoMapper.selectById(productId);
        model.addAttribute("product",p);
        LogObjectHolder.me().set(p);
        return PREFIX + "product_edit.html";
    }

    /**
     * 获取产品信息列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String productType,@RequestParam(required = false) String productMode) {
        Page<ProductInfo> page = new PageFactory<ProductInfo>().defaultPage();
        Integer deptid= ShiroKit.getUser().getDeptId();//获取当前用户部门id
        if(ShiroKit.getUser().getDeptId()==27){//超级管理员
            deptid=null;
        }
        List<Map<String, Object>> result = productDao.getProducts(page ,deptid,productType, productMode ,page.getOrderByField(), page.isAsc()) ;
        page.setRecords((List<ProductInfo>) new ProductWarpper(result).warp());
        return super.packForBT(page);
    }

    /**
     * 获取产品信息列表
     */
    @RequestMapping(value = "/listProductByType")
    @ResponseBody
    public Object listProductByType(@RequestParam(required = false) String productType) {
        Integer deptid= ShiroKit.getUser().getDeptId();//获取当前用户部门id
        if(ShiroKit.getUser().getDeptId()==27){//超级管理员
            deptid=null;
        }
        Map<String,Object> queryMap=new HashMap();
        queryMap.put("deptid",deptid);//部门id
        queryMap.put("product_type",productType);//产品类型
        List result=productInfoMapper.selectByMap(queryMap);
        return result;
    }


    /**
     * 新增产品信息
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(@Valid ProductInfo productInfo, BindingResult result) {
        if(result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        Integer deptid= ShiroKit.getUser().getDeptId();//获取当前用户部门id

        productInfo.setDeptid(deptid);//部门id
        productInfo.setId(null);
        Integer n=productInfoMapper.insertProductInfo(productInfo);
        System.out.println("pid:"+productInfo.getId());
        if(productInfo.getAmount() != null && productInfo.getAmount()>0){//录入初始库存增加记录
            ProductFlow flow=new ProductFlow();
            flow.setPid(productInfo.getId());//产品id
            flow.setBizType(1);//1-入库 2-售出 3-出库
            flow.setDeptid(deptid);//部门id
            flow.setProductModel(productInfo.getProductModel());//产品型号
            flow.setRemarks("初始添加产品入库");
            flow.setAmount(productInfo.getAmount());
            flow.setCreateTime(new Date());
            productFlowMapper.insert(flow);
        }
        return super.SUCCESS_TIP;
    }

    /**
     * 删除产品信息
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer productId) {
        System.out.println("productId:"+productId);
        productInfoMapper.deleteById(productId);
        return SUCCESS_TIP;
    }


    /**
     * 修改产品信息
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(ProductInfo productInfo) {
        if (ToolUtil.isEmpty(productInfo) || productInfo.getId() == null) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        this.productInfoMapper.updateById(productInfo);//更新
        return super.SUCCESS_TIP;
    }


    /**
     * 新增产品信息
     */
    @RequestMapping(value = "/addProductFlow")
    @ResponseBody
    public Object addProductFlow(@Valid ProductFlow productFlow, BindingResult result) {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        ProductInfo info=productInfoMapper.selectById(productFlow.getPid());
        Integer deptid= ShiroKit.getUser().getDeptId();//获取当前用户部门id
        productFlow.setCreateTime(new Date());//创建日期
        productFlow.setDeptid(deptid);//部门id
        productFlow.setProductModel(info.getProductModel());//产品型号
        int n=productFlowMapper.insert(productFlow);
        if(n>0){
            ProductInfo  productInfo=this.productInfoMapper.selectById(productFlow.getPid());
            if(productFlow.getBizType()==1){//入库
                productInfo.setAmount(productInfo.getAmount()+productFlow.getAmount());
            }else  if(productFlow.getBizType()==3){//出库
                productInfo.setAmount(productInfo.getAmount()-productFlow.getAmount());
            }
            productInfoMapper.updateById(productInfo);
        }
        return super.SUCCESS_TIP;
    }

    /**
     * 产品信息详情
     */
    @RequestMapping(value = "/detail")
    @ResponseBody
    public Object detail() {
        return null;
    }
}
